//
// Created by Yuluoyang on 2022/4/2.
//

#include "../headers/ADMIN.h"


void backup_system() {
    while (1) {
        print_menu("[1]立即备份数据\t[2]设置自动备份\t[3]返回主菜单");
        int choice;
        while (!input_int(&choice, 1, 3));
        switch (choice) {
            case 1: {
                printf("请输入您要保存的路径名：");
                char path[128];
                fgets(path, 128, stdin);
                path[strlen(path) - 1] = '\0';
                if (path[strlen(path) - 1] != '/') strcat(path, "/");
                if (access(path, F_OK) == -1) {
                    printf("❓ 路径不存在，要创建吗？\n");
                    print_menu("[1]创建\t[2]返回");
                    while (!input_int(&choice, 1, 2));
                    if (choice == 1) {
                        if (folder(path) == -1) {
                            printf("❌ 创建失败，请检查路径名是否正确\n");
                            continue;
                        }
                    } else {
                        continue;
                    }
                }
                puts("您要备份那些内容？");
                print_menu("[1]全部备份\t[2]部分备份\t[3]返回上一级");
                while (!input_int(&choice, 1, 3));
                if (choice == 3) break;
                bool is_all = choice == 1;
                if (!is_all) {
                    printf("请输入要备份的表名：");
                    print_menu("[1]仓库货物信息\t[2]上架商品信息\t[3]进货记录\t[4]销售记录\t[5]用户和会员信息\t[6]店铺信息\t[7]返回备份系统主菜单");
                    while (!input_int(&choice, 1, 7));
                    if (choice == 7) break;
                }
                switch (choice) {
                    case 1:
                        write_fruitStored(path);
                        if (!is_all) break;
                    case 2:
                        write_fruitOnShelf(path);
                        if (!is_all) break;
                    case 3:
                        write_purchaseMessage(path);
                        if (!is_all) break;
                    case 4:
                        write_sellMessage(path);
                        if (!is_all) break;
                    case 5:
                        write_account(path);
                        if (!is_all) break;
                    case 6:
                        write_shopInfo(path);
                        if (!is_all) break;
                }
                printf("✅ 备份成功!\n");
                break;
            }
            case 2:
                printf(shop.autoBackup ? "✅ 您当前已开启自动备份，要将其关闭吗？\n" : "❎ 您当前未开启自动备份，要开启吗？\n");
                print_menu("[1]是\t[2]否");
                while (!input_int(&choice, 1, 2));
                if (choice == 1) {
                    shop.autoBackup = !shop.autoBackup;
                    printf(shop.autoBackup ? "✅ 自动备份已开启\n" : "❎ 自动备份已关闭\n");
                }
                break;
            case 3:
                return;
        }
    }
}


void sellMessage_manager_system(sellMessage *head1, fruit *head2) {
	while (1) {
		printf("\n");
		print_menu("[1]查看所有销售记录\t[2]查询相应销售记录\t[3]添加相应销售记录\t[4]修改删除销售记录\t[5]退出");//11
		int choice = 0;
		while (!input_int(&choice, 1, 5));
		switch (choice) {
			case 1:
				sellMessage_printf_for_system(head1);
				break;
			case 2:
				sellMessage_find_for_system(head1);
				break;
			case 3:
				sellMessage_add_for_system(head1, head2);
				break;
			case 4:
				data_sellMessage_modify(head_sellMessage);
				break;
			case 5:
				return;
		}
	}
}

void sellMessage_printf_for_system(sellMessage *head) {
	pointer *h = pointer_create();
	struct time_item t = {2000, 1, 1, 0, 0, 0};
	print_menu("[1]按销售时间由近到远输出\t[2]按照消费额由低到高排序\t[3]按照消费件数由低到高");
	int temp = 0;
	while (!input_int(&temp, 1, 3));
	for (sellMessage *p = head->next; p != NULL; p = p->next) {
		if (temp == 3)
			pointer_add(h, p, 2, p->number);
		if (temp == 2)
			pointer_add(h, p, 2, p->totalPrice);
		if (temp == 1)
			pointer_add(h, p, 2, get_dayPassed(p->sellTime, t));
	}
	pointer_sort(h,false);
	print_sell(h);
	pointer_free(h);
}

void sellMessage_find_for_system(sellMessage *head) {
    while (1) {
        pointer *h = pointer_create();
        printf("请选择你的操作\n");
        print_menu("[1]按购买数量查询\t[2]按时间查询\t[3]按顾客查询\t[4]返回上一级");//111
        int choice = 0;
        while (!input_int(&choice, 1, 4));
        switch (choice) {
            case 1: {
                int number;
                bool flag = false;
                printf("请输入要查询的数量\n");
                while (!input_int(&number, 0, 0));
                for (sellMessage *p = head; p != NULL; p = p->next)
                    if (p->number == number) {
                        pointer_add(h, p, 2, p->number);
                        flag = true;
                    }
                if (flag)
                    print_sell(h);
                else
                    printf("没有找到相关记录\n");
            }
                break;
            case 2: {
                sellMessage *temp;
	            temp = (sellMessage *) malloc(sizeof(sellMessage));
	            printf("请输入要查询的时间 输入格式yyyy/mm/dd \n");
                while (1) {
	                if (scanf("%d/%d/%d", &temp->sellTime.year, &temp->sellTime.month, &temp->sellTime.day) == 6) {
		                while (getchar() != '\n')
		                for (sellMessage *p = head; p != NULL; p = p->next)
			                if (p->sellTime.year == temp->sellTime.year && p->sellTime.month == temp->sellTime.month &&
			                    p->sellTime.day == temp->sellTime.day)
				                pointer_add(h, p, 2, p->number);
		                print_sell(h);
		                break;
	                } else
		                printf("输入错误 请重新输入\n");
                }
            }
                break;
            case 3: {
                char name[32];
                bool flag = false;
                printf("请输入要查询的顾客\n");
                while (!input_string(name, 32));
                for (sellMessage *p = head->next; p != NULL; p = p->next)
                    if (strcmp(p->customer, name) == 0) {
                        flag = true;
                        pointer_add(h, p, 2, p->number);
                    }
                if (flag)
                    print_sell(h);
                else
                    printf("没有找到相关记录\n");
            }
            case 4:
	            pointer_free(h);
                return;
        }
        pointer_free(h);
    }
}

void sellMessage_add_for_system() {
    sellMessage item;
    printf("请输入顾客姓名\n");
    while (!input_string(item.customer, 32));
    printf("请输入购买时间（回车使用当前时间）输入格式YYYY/MM/DD hh:mm:ss\n");
    while (1) {
        char t = getchar();
        if (t == '\n') {
            get_timeItem(&item.sellTime);
	        printf("当前时间为%d/%d/%d %d:%d:%d\n", item.sellTime.year, item.sellTime.month, item.sellTime.day,
	               item.sellTime.hour, item.sellTime.minute, item.sellTime.second);
            break;
        }
        if (scanf("%d/%d/%d %d:%d:%d", &item.sellTime.year, &item.sellTime.month, &item.sellTime.day,
                  &item.sellTime.hour, &item.sellTime.minute, &item.sellTime.second) == 6) {
            item.sellTime.year += (t - '0') * 1000;
            while (getchar() != '\n');
            break;
        } else {
            while (getchar() != '\n');
            puts("输入错误 请重新输入：\n");
        }
    }
    printf("请输入购买件数\n");
    while (!input_int(&item.number, 0, 0));
    item.head = fruit_create();
    for (int i = 1; i <= item.number; i++) {
        pointer *p = pointer_create();
        printf("下面请在上架商品中检索第%d件：\n",i);
	    shelf_find(head_fruitOnShelf, p);
		if(p->next!=NULL){
	    int number = 0;
	    for (pointer *j = p->next; j != NULL; j = j->next, number++);
	    printf("请选择您要添加的商品序号：\n");
	    while (!input_int(&number, 1, number));
	    pointer *temp = p;
	    while (number--) temp = temp->next;
	    fruit target = *(temp->p_fruit);
	    printf("请输入购买数量：\n");
	    while (!input_double(&target.kilo));
	    free(p);
	    fruit_add(item.head, target);
	    pointer_free(temp);}
		else
			i--;
    }
    puts("以下为您此次添加的销售记录\n");
    pointer *temp = pointer_create();
    pointer_add(temp, &item, 2, item.number);
    print_sell(temp);
    sellMessage_add(head_sellMessage, item);
	pointer_free(temp);
}

void money_manager_system(sellMessage *head1, purchaseMessage *head2) {
	while (1) {
		print_menu("[1]查询销售进账\t[2]查询进货出账\t[3]查询当前的总money\t[4]查询销售员营业额\t[5]返回上一级");//111
		int choice;
		while (!input_int(&choice, 1, 5));
		switch (choice) {
			case 1:
				sellMessage_money_manager(head1);
				break;
			case 2:
				purchaseMessage_money_manager(head2);
				break;
			case 3:
				i_shop_money_manager(head1, head2);
				break;
            case 4:
                SALES_money_printf();
                break;
			case 5:
				return;
		}
	}
}

void sellMessage_money_manager(sellMessage *head) {
    while (1) {
	    print_menu("[1]查询果蔬品种的销售进账\t[2]查询具体果蔬名称的销售进账\t[3]查询指定日期后销售的进账\t[4]查询全部的销售进账\t[5]返回上一级");//111
	    int choice;
	    pointer *h = pointer_create();
	    while (!input_int(&choice, 1, 5));
	    switch (choice) {
		    case 1: {
			    char keyword[64];
			    printf("请输入查询的品种：\n");
			    while (!input_string(keyword, 64));
			    sellMessage *p;
			    bool flag = false;
			    for (p = head->next; p != NULL; p = p->next) {
				    for (fruit *q = p->head; q != NULL; q = q->next) {
					    if (strcmp(q->type, keyword) == 0) {
						    pointer_add(h, q, 0, q->unitPrice);
						    flag = true;
					    }
				    }
			    }
			    if (flag) {
				    double num = 0;
				    for (pointer *q = h->next; q != NULL; q = q->next)
					    num += q->p_fruit->unitPrice * q->p_fruit->kilo;
				    printf("查询累型进账总结果如下：\n%.2lf\n", num);
				    print_menu("[1]退出\t[2]继续查询");//1111
				    int temp = 0;
				    while (!input_int(&temp, 1, 2));
				    if (temp == 1) {
					    pointer_free(h);
					    return;
				    }
			    } else {
				    printf("没有查询到相关数据\n");
                    break;
                }
            }
                break;
            case 2: {
	            char keyword[64];
	            printf("请输入查询的具体果蔬名称：\n");
	            while (!input_string(keyword, 64));
	            sellMessage *p;
	            bool flag;
	            for (p = head->next; p != NULL; p = p->next) {
		            for (fruit *q = p->head; q != NULL; q = q->next) {
			            if (strcmp(q->name, keyword) == 0) {
				            pointer_add(h, q, 0, q->unitPrice);
				            flag = true;
			            }
		            }
	            }
	            if (flag) {
		            double num = 0;
		            for (pointer *q = h->next; q != NULL; q = q->next)
			            num += q->p_fruit->unitPrice * q->p_fruit->kilo;
		            printf("查询结果如下：\n%.2lf\n", num);
		            print_menu("[1]退出\t[2]继续查询");
		            int temp = 0;
		            while (!input_int(&temp, 1, 2));
		            if (temp == 1) {
			            pointer_free(h);
			            return;
		            }
	            } else {
		            printf("没有查询到相关数据\n");
                    break;
                }
            }
                break;
            case 3: {
	            sellMessage *p0 = (sellMessage *) malloc(sizeof(sellMessage));
	            double num = 0;
	            printf("请输入查询的时间：按照格式yyyy/mm/dd \n");
	            while (1) {
		            if (scanf("%d/%d/%d", &p0->sellTime.year, &p0->sellTime.month, &p0->sellTime.day)) {
			            for (sellMessage *p = head->next; p != NULL; p = p->next) {
				            if (get_dayPassed(p0->sellTime, p->sellTime) > 0)
					            num += p->totalPrice;
			            }
			            printf("查询结果如下：\n%.2lf\n", num);
			            print_menu("[1]退出\t[2]继续查询");
			            int temp = 0;
			            while (!input_int(&temp, 1, 2));
			            if (temp == 1) {
				            pointer_free(h);
				            return;
			            }
			            if (temp == 2)
				            break;
		            } else
			            puts("输入错误 请重新输入：");
		            while (getchar() != '\n');
	            }
	            break;
            }
		    case 4: {
			    double num = 0;
			    for (sellMessage *p = head_sellMessage->next; p != NULL; p = p->next)
				    num += p->totalPrice;
			    printf("当前总的销售进账为：%.2lf\n", num);
		    }
		    case 5:
			    pointer_free(h);
			    return;
	    }
	    pointer_free(h);
    }
}

void purchaseMessage_money_manager(purchaseMessage *head) {
    while (1) {
	    print_menu("[1]查询品种的进货出账\t[2]查询具体果蔬的进货出账\t[3]按照指定时间后查询进货出账\t[4]查询全部进货出账\t[5]返回上一级");//111
	    int choice;
	    pointer *h = pointer_create();
	    while (!input_int(&choice, 1, 5));
	    switch (choice) {
		    case 1: {
			    char keyword[32];
			    printf("请输入查询的品种：\n");
			    while (!input_string(keyword, 32));
			    purchaseMessage *p;
			    bool flag;
			    for (p = head->next; p != NULL; p = p->next) {
				    if (strcmp(p->type, keyword) == 0) {
					    pointer_add(h, p, 1, p->totalPrice);
					    flag = true;
				    }
			    }
			    if (flag) {
				    double num = 0;
				    for (pointer *q = h->next; q != NULL; q = q->next)
					    num += q->p_purchase->totalPrice;
				    printf("查询累型进账总结果如下：\n%.2lf\n", num);
				    print_menu("[1]退出\t[2]继续查询");
				    int temp = 0;
				    while (!input_int(&temp, 1, 2));
				    if (temp == 1) {
					    pointer_free(h);
					    return;
				    }
			    } else {
				    printf("没有查询到相关数据\n");
				    break;
			    }
            }
                break;
            case 2: {
	            char keyword[64];
	            printf("请输入查询的具体果蔬名称：\n");
	            while (!input_string(keyword, 32));
	            purchaseMessage *p;
	            bool flag = false;
	            for (p = head->next; p != NULL; p = p->next) {
		            if (strcmp(p->name, keyword) == 0) {
			            pointer_add(h, p, 1, p->totalPrice);
			            flag = true;
		            }
	            }
	            if (flag) {
		            double num = 0;
		            for (pointer *q = h->next; q != NULL; q = q->next)
			            num += q->p_purchase->totalPrice;
		            printf("查询结果如下：\n%.2lf\n", num);
		            print_menu("[1]退出\t[2]继续查询");//111
		            int temp = 0;
		            while (!input_int(&temp, 1, 2));
		            if (temp == 1) {
			            pointer_free(h);
			            return;
		            }
	            } else printf("没有查询到相关数据\n");
	            break;
            }
            case 3: {
                purchaseMessage *p0;
                double num = 0;
                p0 = (purchaseMessage *) malloc(sizeof(purchaseMessage));
                printf("请输入查询的时间：按照格式yyyy/mm/dd\n");
                if (scanf("%d/%d/%d", &p0->purchaseTime.year, &p0->purchaseTime.month, &p0->purchaseTime.day) == 3) {
	                for (purchaseMessage *p = head->next; p != NULL; p = p->next) {
                        if (get_dayPassed(p0->purchaseTime, p->purchaseTime) > 0)
		                num += p->totalPrice;
	                }
	                printf("查询结果如下：\n%.2lf\n", num);
	                print_menu("[1]退出\t[2]继续查询");//111111111111111111111111111111111111111111111
	                int temp = 0;
	                while (!input_int(&temp, 1, 2));
	                if (temp == 1) {
		                pointer_free(h);
		                return;
	                }
                } else
	                puts("输入错误 请重新输入：\n");
	            while (getchar() != '\n');
	            break;
            }
		    case 4: {
			    double num = 0;
			    for (purchaseMessage *p = head_purchaseMessage->next; p != NULL; p = p->next)
				    num += p->totalPrice;
			    printf("当前总的进货出账为：%.2lf\n", num);
			    break;
		    }
		    case 5:
			    pointer_free(h);
			    return;
	    }
	    pointer_free(h);
    }
}

void i_shop_money_manager(sellMessage *head1, purchaseMessage *head2) {
	double num1 = 0, num2 = 0;
	for (purchaseMessage *p = head2->next; p != NULL; p = p->next) {
		num2 += p->totalPrice;
	}
	for (sellMessage *p = head1->next; p != NULL; p = p->next) {
		num1 += p->totalPrice;
	}
	printf("当前的金额为：%.2lf\n", shop.startMoney + num1 - num2);
}

